/**
 * Created by Dalyn on 2017/5/8.
 * 生产排期
 */
import React from "react";
import {hashHistory} from 'react-router';
import Relay from "react-relay";
import InputBox from "../../../common/InputBox"
import Pagination from "../Pagination"
import { Carousel,message,Icon,Select,Spin} from 'antd';
import {Button} from 'fly';
const Option = Select.Option;
import ReactKey from "../../../util/ReactKey";
import Util from "../../../util/Util";
import ProductionScheduleServerMutation from '../mutation/productionScheduling/ProductionScheduleServerMutation';
import Logic_Detail from './component/Logic_Detail';
import "../css/productionScheduling.css";
import "slick-carousel";

export default class ProductionScheduling extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
            editable: true,
            value:'',
            jsonData: [],
            search_jsonData: [],
            pageSize:"",
            styleNo_i:"",
            enNameError:"",
            customerName:"",
            searchOrderFlag:true,
            searchCustomerFlag:true,
            int:0,
            cur_psId:"",
            search_styleNo:"",
            search_orderId:"",
            styleNoDisplay:true,
            styleNoHint:"",
            orderIdHint:"",
        };
        this.style = {
            input:{
                width: "200px",
                height: "28px",
            },
        };
        this.orderType={
            1:"成衣",
            2:"辅料",
            3:"面料",
            4:"印染",
            5:"纱线",
        };
        this.messageHint =true;
        this.orderGoodss =[];
        this.enterpriseId = sessionStorage.getItem("enterpriseId");
        this.roleNumber = sessionStorage.getItem("roleNumber");
        this.operationId = sessionStorage.getItem("uid");
    }
    componentWillMount(){
        this.proDigestList({
            action: "styleNoList",
            enId:this.enterpriseId,
            styleNo:"",
        });

        // this.httpRequest({},this.TableDataFinishing);
    }
    /*查询数据*/
    proDigestList(params,cb){
        Relay.Store.commitUpdate(
            new ProductionScheduleServerMutation(params),
            {
                onFailure: () => {
                    message.error('网络请求失败');
                },
                onSuccess: (response) => {
                    let errorCode = response.productionScheduleServer.responseData.errorCode;
                    let value = response.productionScheduleServer.responseData.value;
                    if(errorCode=='0'){
                        switch (params.action){
                            case "styleNoList":
                                let jsonData = response.productionScheduleServer.responseData.jsonData;
                                jsonData = JSON.parse(jsonData);
                                // console.log(jsonData);
                                /*假数据*/
                                let jsonData_1 = [
                                    {"psId":101,"styleNo":"aaa","orderIds":["123123","123123"]},
                                    {psId:"11",styleNo:"你不是我的你","orderIds":["aaaaaaaa","1231sssssss23"]},
                                    {psId:"111",styleNo:"我不是你的我","orderIds":["sfgsfgsdfgsdfg","1231hfghjfhjfg23"]},
                                    {psId:"1111",styleNo:"我不是","orderIds":["ghjfghj123123","fghjfghj123123"]},
                                    {psId:"11111",styleNo:"我不是啊啊啊啊啊啊啊","orderIds":["fghjf","12hjhjhjhj3123"]},
                                    {psId:"111111",styleNo:"我是你是我"},
                                    {psId:"121",styleNo:"我是你是我"},
                                    {psId:"12",styleNo:"我是你是我"},
                                    {psId:"122",styleNo:"我是你是我"},
                                    {psId:"1222",styleNo:"我是你是我"},
                                    {psId:"12222",styleNo:"我是你是我"},
                                    {psId:"122222",styleNo:"我是你是我"},
                                    {psId:"13",styleNo:"我是你是我"},
                                    {psId:"133",styleNo:"我是你是我"},
                                    {psId:"1333",styleNo:"我是你是我"},
                                    {psId:"13333",styleNo:"我是你是我"},
                                    {psId:"3331",styleNo:"我是你是我"},
                                    {psId:"1342",styleNo:"我是你是我"},
                                    {psId:"123423",styleNo:"123456"},
                                    {psId:"12342",styleNo:"你不是我的你"},
                                    {psId:"3451",styleNo:"我不是你的我"},
                                    {psId:"15763",styleNo:"我不是"},
                                    {psId:"781",styleNo:"我不是啊啊啊啊啊啊啊"},
                                    {psId:"7856781",styleNo:"我是你是我"},
                                    {psId:"1567856",styleNo:"我是你是我"},
                                    {psId:"57895891",styleNo:"我是你是我"},
                                    {psId:"967891",styleNo:"我是你是我"},
                                    {psId:"197897689",styleNo:"我是你是我"},
                                    {psId:"1456000",styleNo:"我是你是我"},
                                    {psId:"15675",styleNo:"我是你是我"},
                                    {psId:"14564",styleNo:"我是你是我"},
                                    {psId:"1676786",styleNo:"我是你是我"},
                                    {psId:"1458645",styleNo:"我是你是我"},
                                    {psId:"175675",styleNo:"我是你是我"},
                                    {psId:"1ghjfh",styleNo:"我是你是我"},
                                    {psId:"7867861",styleNo:"我是你是我"},
                                    {psId:"176867",styleNo:"123456"},
                                    {psId:"hbgnc1",styleNo:"你不是我的你"},
                                    {psId:"167567",styleNo:"我不是你的我"},
                                    {psId:"1786786",styleNo:"我不是"},
                                    {psId:"1ykghk",styleNo:"我不是啊啊啊啊啊啊啊"},
                                    {psId:"1fgfghd",styleNo:"我是你是我"},
                                    {psId:"1sdfs",styleNo:"我是你是我"},
                                ];
                                if(params.styleNo){//搜索
                                    this.setState({
                                        search_jsonData:jsonData?JSON.parse(JSON.stringify(jsonData)).splice(0,4):[],
                                    });
                                }else {
                                    this.setState({
                                        jsonData:jsonData?jsonData:[],
                                        cur_psId:jsonData&&jsonData.length>0&&jsonData[0].psId,
                                    });
                                }
                                break;
                            case "descOS"://点击搜索
                                let productionScheduleModel = response.productionScheduleServer.responseData.productionScheduleModel;
                            this.setState({
                                cur_psId:productionScheduleModel.psId,
                            });
                                break;
                        }
                    }else {
                        if (errorCode == 500){
                            message.error('服务器错误,请联系系统管理员');
                        }else {
                            message.error(value)
                        }
                    }
                }
            }
        )
    }
    /*文本变化回调*/
    selectSearch(type,value){
        // console.log(value, type);
        switch (type){
            case "styleNo":
                this.proDigestList({
                    action: "styleNoList",
                    styleNo: value?value:"",
                    enId:this.enterpriseId,
                    pageSize:"4",
                });
                break;
            case "orderId":

                break;
        }
    }
    selectChange(type,value){
        // console.log(type, value);
        let {search_jsonData} = this.state;
        switch (type){
            case "styleNo":
                this.setState({
                    search_styleNo:search_jsonData[value].styleNo,
                    styleNo_i:value,
                    styleNoHint:"",
                });
                break;
            case "orderId":
                this.setState({
                    search_orderId:value,
                    orderIdHint:"",
                });
                break;
        }
    }
    /*搜索*/
    search(){
        let this_ = this;
        let {search_styleNo,search_orderId} = this.state;
        // console.log(search_styleNo,search_orderId);
        if(!search_styleNo){
            this.setState({
                styleNoHint:"请选择款号"
            });
            return
        }else {
            this.setState({
                styleNoHint:""
            });
        }
        if(!search_orderId){
            this.setState({
                orderIdHint:"请选择订单"
            });
            return
        }else {
            this.setState({
                orderIdHint:""
            });
        }
        this.proDigestList({
            action: "descOS",
            enId:this.enterpriseId,
            styleNo:search_styleNo,
            orderId:search_orderId,
        });
        //款号隐藏
        this.setState({
            styleNoDisplay:false,
        });
        //加载动画
        this.refs.Logic_Detail.setState({
            spinning:true,
        })
    }
    /*调整生产任务*/
    editProductionTask(){
        hashHistory.push({
            pathname:"/createProductionScheduling/",
            state:{}
        })
    }
    /*新建生产排期*/
    newProductionSchedule(){
        hashHistory.push({
            pathname:"/createProductionScheduling/",
            state:{}
        })
    }
    /*点击款号*/
    clickStyleNo(psId){
        // console.log(psId);
        this.setState({
            cur_psId:psId,
        });
        this.refs.Logic_Detail.setState({
            spinning:true,
        })
    }
    /*点击返回*/
    searchBack(){
        this.setState({
            styleNoDisplay:true,
        })
    }
    /*点击产量详情*/
    outPutDetail(){
        let data_t = this.refs.Logic_Detail.state.tableData[0];
        hashHistory.push({pathname:"/ProgressDetail/",state:{
            defaultActiveKey:'2',
            record:{
                orderId:data_t.orderId, //订单号
                styleNo:data_t.styleNo, //款号
                agreement:data_t.quantity, //合同数
                countTailor:data_t.complete,  //累计缝制数
            },
        }})
    }
    render(){
        let {jsonData,search_jsonData,styleNo_i,search_orderId,cur_psId,styleNoDisplay,styleNoHint,orderIdHint} = this.state;
        let jsonData_arr = JSON.parse(JSON.stringify(jsonData));
        let orderIds = search_jsonData&&search_jsonData[styleNo_i]&&search_jsonData[styleNo_i].orderIds?search_jsonData[styleNo_i].orderIds:[];
        // console.log(cur_psId);
        let slide_arr = [];
        let styleNoFun = ()=>
        {
            let  styleNo_arr = jsonData_arr.splice(0,7);
            slide_arr.push(styleNo_arr);
            if(jsonData_arr.length<=7)return;
            return styleNoFun();
        };
        styleNoFun();
        //console.log(slide_arr.length);
        const prevArrow = ()=>{
            return (
                <span className="production_scheduling_carousel_iconSpan">
                    <Icon className="production_scheduling_carousel_icon" type="left" />
                </span>
            )
        };
        const nextArrow = ()=>{
            return (
                <span className="production_scheduling_carousel_iconSpan">
                    <Icon className="production_scheduling_carousel_icon" type="right" />
                </span>
            )
        };
        return (
            <div className="poduction_manage_box">
                <div className="production_scheduling_head_search clear">
                    <div className="production_scheduling_head_searchInp" ref={"styleNoBox"}>
                        <span className="production_scheduling_hint_word">款号：</span>
                        <span className="production_scheduling_hint_left">
                            <Select
                                    key={"styleNo"}
                                    showSearch
                                    style={{ width: 200 }}
                                    className="order_creat_select"
                                    dropdownClassName="order_creat_select_drop"
                                    placeholder="请输入款号"
                                    notFoundContent="无该款号"
                                    optionFilterProp="children"
                                    getPopupContainer={()=>this.refs.styleNoBox}
                                    defaultActiveFirstOption={false}
                                    showArrow={false}
                                    onSearch={this.selectSearch.bind(this,"styleNo")}
                                    onChange={this.selectChange.bind(this,"styleNo")}
                                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                                >
                                    {
                                        search_jsonData&&search_jsonData.map((v,i)=>{
                                            return(
                                                <Option key={v.styleNo} value={i}>{v.styleNo}</Option>
                                            )
                                        })
                                    }
                                </Select>
                            <span className="production_scheduling_hint_text">{styleNoHint}</span>
                        </span>
                    </div>
                    <div className="production_scheduling_head_searchInp" ref={"orderIdBox"}>
                        <span className="production_scheduling_hint_word">订单编号：</span>
                        <span className="production_scheduling_hint_left">
                            <Select
                                    key={"orderId"}
                                    showSearch
                                    style={{ width: 200 }}
                                    className="order_creat_select"
                                    dropdownClassName="order_creat_select_drop"
                                    placeholder="请选择订单编号"
                                    notFoundContent={styleNo_i?"该款号无对应订单":"请先选择款号"}
                                    optionFilterProp="children"
                                    getPopupContainer={()=>this.refs.orderIdBox}
                                    defaultActiveFirstOption={false}
                                    showArrow={true}
                                    //onSearch={this.selectSearch.bind(this,"orderId")}
                                    onChange={this.selectChange.bind(this,"orderId")}
                                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                                >
                                    {
                                        orderIds.map((v,i)=>{
                                            return(
                                                <Option key={v} value={v}>{v}</Option>
                                            )
                                        })
                                    }
                                </Select>
                            <span className="production_scheduling_hint_text">{orderIdHint}</span>
                        </span>
                    </div>
                    <div className="production_scheduling_head_searchBtn">
                        <Button  onClick={this.search.bind(this)}>
                            搜索
                        </Button>
                        <span className="production_scheduling_head_searchHint">{this.state.enNameError}</span>
                    </div>
                    <div className="production_scheduling_head_btnBox">
                        <Button className="production_scheduling_head_btn" onClick={this.editProductionTask.bind(this)}>
                            调整生产任务
                        </Button>
                        <Button className="production_scheduling_head_btn" onClick={this.newProductionSchedule.bind(this)}>
                            新建生产排期
                        </Button>
                    </div>
                </div>
                <div style={styleNoDisplay?{display:"block"}:{display:"none"}} className="production_scheduling_carousel_box">
                    <Carousel
                        className="production_scheduling_carousel"
                        //autoplay
                        dots = {false}
                        arrows={true}
                        prevArrow={prevArrow()}
                        nextArrow={nextArrow()}
                    >
                        {
                            slide_arr && slide_arr.map((v,i)=>{
                                /*滑块数组*/
                                return(
                                    <div className="production_scheduling_carousel_slide">
                                        {
                                            v && v.map((item,index)=>{
                                                /*款号*/
                                                return (
                                                    <span className={cur_psId == item.psId?"production_scheduling_carousel_span styleNo_background":"production_scheduling_carousel_span"}
                                                        onClick={this.clickStyleNo.bind(this,item.psId)}>
                                                        {item.styleNo}
                                                        </span>
                                                )
                                            })
                                        }
                                    </div>
                                )
                            })
                        }
                    </Carousel>
                </div>
                <div className="production_scheduling_head_btn_outBox">
                    <span style={styleNoDisplay?{display:"none"}:{display:"block"}} className="production_scheduling_head_btn_back">
                        <Button  onClick={this.searchBack.bind(this)}>
                            返回
                        </Button>
                    </span>

                    <span className="production_scheduling_head_btn_out" onClick={this.outPutDetail.bind(this)}>
                        产量详情
                    </span>
                </div>

                <div>
                    <Logic_Detail
                        ref={"Logic_Detail"}
                        psId={cur_psId}
                    />

                    <p className="production_scheduling_chart_des_p">
                       <span className="production_scheduling_chart_des_sp">图例说明：</span>
                        <span className="production_scheduling_chart_des_sp">
                            <i className="production_scheduling_chart_des_ir"></i>未达到计划平均产量
                        </span>
                        <span className="production_scheduling_chart_des_sp">
                            <i className="production_scheduling_chart_des_ig"></i>达到或超过计划平均产量
                        </span>
                    </p>
                </div>
            </div>
        )
    }
}
